<?php

namespace Louis\Router;
use Louis\Container;
use Louis\CallableResolver;

class RouteCollector {
    //use RouteStrategyTrait;
    /** @var RouteParser */
    protected $routeParser;

    /** @var DataGenerator */
    protected $dataGenerator;

    /** @var string */
    protected $currentGroupPrefix;

    /**
     * @var Container
     */
    protected $container;
    /**
     * Constructs a route collector.
     *
     * @param Container $container
     * @param RouteParser   $routeParser
     * @param DataGenerator $dataGenerator
     */
    public function __construct(Container $container = null, RouteParser $routeParser = null, DataGenerator $dataGenerator = null) {
        $this->container = $container ? : new Container();
        $this->routeParser = $routeParser ? : new RouteParser();
        $this->dataGenerator = $dataGenerator ? : new DataGenerator();
        $this->currentGroupPrefix = '';
    }

    /**
     * Adds a route to the collection.
     *
     * The syntax used in the $route string depends on the used route parser.
     *
     * @param string|string[] $httpMethod
     * @param string $route
     * @param mixed  $handler
     */
    public function addRoute($httpMethod, $route, $handler) {
        $route = $this->currentGroupPrefix . $route;
        $routeDatas = $this->routeParser->parse($route);

        foreach ((array) $httpMethod as $method) {
            if (is_string($handler) && strpos($handler, '::') !== false) {
                $callback = explode('::', $handler);
            }else{
                $callback = [$handler];
            }
//            $callback = $handler;
//            var_dump($callback);
//            $resolver = new CallableResolver($this->container);
//            $resolved = $resolver->resolve($callback);
//            var_dump($this->container);die;
            foreach ($routeDatas as $routeData) {
//                $handler  = uniqid('route::', true);
//                if (is_string($callback) && strpos($callback, '::') !== false) {
//                    $controller = explode('::', $callback);
//                    $this->container->add($handler, $controller[0], true);
//                }

                $this->container->add($handler, $callback[0], true)
                    ->withMethodCall("setRequest", ["request"])
                    ->withMethodCall("setResponse", ["response"])
                    ->withMethodCall("setContainer", [$this->container]);
                $this->dataGenerator->addRoute($method, $routeData, $handler);
            }
        }

    }

    /**
     * Create a route group with a common prefix.
     *
     * All routes created in the passed callback will have the given group prefix prepended.
     *
     * @param string $prefix
     * @param callable $callback
     */
    public function addGroup($prefix, callable $callback) {
        $previousGroupPrefix = $this->currentGroupPrefix;
        $this->currentGroupPrefix = $previousGroupPrefix . $prefix;
        $callback($this);
        $this->currentGroupPrefix = $previousGroupPrefix;
    }

    /**
     * Adds a GET route to the collection
     *
     * This is simply an alias of $this->addRoute('GET', $route, $handler)
     *
     * @param string $route
     * @param mixed  $handler
     */
    public function get($route, $handler) {
        $this->addRoute('GET', $route, $handler);
    }

    /**
     * Adds a POST route to the collection
     *
     * This is simply an alias of $this->addRoute('POST', $route, $handler)
     *
     * @param string $route
     * @param mixed  $handler
     */
    public function post($route, $handler) {
        $this->addRoute('POST', $route, $handler);
    }

    /**
     * Adds a PUT route to the collection
     *
     * This is simply an alias of $this->addRoute('PUT', $route, $handler)
     *
     * @param string $route
     * @param mixed  $handler
     */
    public function put($route, $handler) {
        $this->addRoute('PUT', $route, $handler);
    }

    /**
     * Adds a DELETE route to the collection
     *
     * This is simply an alias of $this->addRoute('DELETE', $route, $handler)
     *
     * @param string $route
     * @param mixed  $handler
     */
    public function delete($route, $handler) {
        $this->addRoute('DELETE', $route, $handler);
    }

    /**
     * Adds a PATCH route to the collection
     *
     * This is simply an alias of $this->addRoute('PATCH', $route, $handler)
     *
     * @param string $route
     * @param mixed  $handler
     */
    public function patch($route, $handler) {
        $this->addRoute('PATCH', $route, $handler);
    }

    /**
     * Adds a HEAD route to the collection
     *
     * This is simply an alias of $this->addRoute('HEAD', $route, $handler)
     *
     * @param string $route
     * @param mixed  $handler
     */
    public function head($route, $handler) {
        $this->addRoute('HEAD', $route, $handler);
    }

    /**
     * Returns the collected route data, as provided by the data generator.
     *
     * @return array
     */
    public function getData() {
        return $this->dataGenerator->getData();
    }


    /**
     * Builds a dispatcher based on the routes attached to this collection
     *
     * @return Dispatcher
     */
    public function getDispatcher()
    {
        $dispatcher = new Dispatcher($this->container, $this->getData());
        return $dispatcher;
    }
}
